﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
namespace @Model.name_space
{
    /// <summary>
    /// @((Model.Description+"").Replace("\r","").Replace("\n",""))
    ///</summary>
    [SugarTable("@(Model.TableName)")]
    public partial class @(Model.ClassName)Entity:IBaseModelEntity
    {
        public @(Model.ClassName)Entity()
        {
            this.CreateTime = DateTime.Now;
        }
        [JsonConverter(typeof(ValueToStringConverter))]
        [SugarColumn(ColumnName="Id" ,IsPrimaryKey = true   )]
         public long Id { get; set; }
        @foreach (var item in Model.PropertyGens)
        {
            var isPrimaryKey = item.IsPrimaryKey ? ",IsPrimaryKey = true" : "";
            var isIdentity = item.IsIdentity ? ",IsIdentity = true" : "";
            var isNull=(item.IsNullable&&item.Type!="string"&&item.IsSpecialType==false&&item.Type!="byte[]")?"?":"";
            var attrIsNullable =item.Type=="string"&&item.IsNullable ? ",IsNullable = true" : "";
            var isIgnore=(item.IsIgnore?",IsIgnore = true":"");
            var isJson=(item.CodeType.StartsWith("json")?",IsJson= true":"");

            var newPropertyName=item.PropertyName; //这里可以用C#处理 实体属性的显式格式
            //想和数据库一样就用 newPropertyName=item.DbColumnName
            List<string> isIgnoreList=new List<string>()
            {
                "Id"
            };
            if (isIgnoreList.Contains(newPropertyName))
            {
                continue;
            }
            if(System.Text.RegularExpressions.Regex.IsMatch(newPropertyName.Substring(0,1), "[0-9]"))
            {
                newPropertyName="_"+newPropertyName;//处理属性名开头为数字情况
            }
            if(newPropertyName==Model.ClassName)
            {
                newPropertyName="_"+newPropertyName;//处理属性名不能等于类名
            }
            var desc=(item.Description+"").Replace("\r","").Replace("\n","");//处理换行
            if(isIgnore!="")
            {
            isPrimaryKey =isIdentity =isNull=attrIsNullable="";
            }
            @:/// <summary>
            @:/// @(desc) 
            @if(item.DefaultValue!=null){
            @:/// 默认值: @Raw(item.DefaultValue)
            }
            @:///</summary>
            @: [SugarColumn(ColumnName="@item.DbColumnName"@(isPrimaryKey)@(isIdentity)@(attrIsNullable)@(isIgnore)@(isJson))]
            @: public @Raw(item.Type)@isNull @newPropertyName { get; set; }
            if(newPropertyName.EndsWith("Id")&&Model.Classes.Where(c=>c==newPropertyName.Replace("Id","")).Any()) //自动关联外键
            {
                var fk=newPropertyName.Replace("Id","");
                @:[Navigate( NavigateType.OneToOne,nameof(@newPropertyName))]
                @:public @(fk)Entity @(fk) { get; set; }
            }
        }
        @if(!Model.PropertyGens.Where(p=>p.PropertyName=="CreateTime").Any()){
            @:/// <summary>
            @:///  
            @:///</summary>
            @:[SugarColumn(ColumnName = "CreateTime")]
            @:public DateTime? CreateTime { get; set; }
        }
        @if(!Model.PropertyGens.Where(p=>p.PropertyName=="ModifyTime").Any()){
            @:/// <summary>
            @:///  
            @:///</summary>
            @:[SugarColumn(ColumnName = "ModifyTime")]
            @:public DateTime? ModifyTime { get; set; }
        }
        @if(!Model.PropertyGens.Where(p=>p.PropertyName=="IsDeleted").Any()){
            @:/// <summary>
            @:///  
            @:///</summary>
            @:[SugarColumn(ColumnName = "IsDeleted")]
            @:public bool? IsDeleted { get; set; }
        }         
    }
}
